﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using Maticsoft.DBUtility;//Please add references
namespace hcrainMvc.DAL
{
	/// <summary>
	/// 数据访问类:hsc_task
	/// </summary>
	public partial class hsc_task
	{
		public hsc_task()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(long id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from hsc_task");
			strSql.Append(" where id=@id");
			MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int64)
			};
			parameters[0].Value = id;

			return DbHelperMySQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(hcrainMvc.Model.hsc_task model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into hsc_task(");
			strSql.Append("gid,type,taskno,taskinfo,tasksctype,tasksctypename,taskuserid,taskmj,taskcj,taskendtime,newwork,newworktime,newworkuserid,taskstatus,newworkusername,dataadduserid,datadepid,databranchid,datadepname,dataaddusername,databranchname,dataaddtime)");
			strSql.Append(" values (");
			strSql.Append("@gid,@type,@taskno,@taskinfo,@tasksctype,@tasksctypename,@taskuserid,@taskmj,@taskcj,@taskendtime,@newwork,@newworktime,@newworkuserid,@taskstatus,@newworkusername,@dataadduserid,@datadepid,@databranchid,@datadepname,@dataaddusername,@databranchname,@dataaddtime)");
			MySqlParameter[] parameters = {
					new MySqlParameter("@gid", MySqlDbType.VarChar,36),
					new MySqlParameter("@type", MySqlDbType.VarChar,10),
					new MySqlParameter("@taskno", MySqlDbType.VarChar,50),
					new MySqlParameter("@taskinfo", MySqlDbType.VarChar,800),
					new MySqlParameter("@tasksctype", MySqlDbType.Int64,20),
					new MySqlParameter("@tasksctypename", MySqlDbType.VarChar,50),
					new MySqlParameter("@taskuserid", MySqlDbType.Int64,20),
					new MySqlParameter("@taskmj", MySqlDbType.Double),
					new MySqlParameter("@taskcj", MySqlDbType.Double),
					new MySqlParameter("@taskendtime", MySqlDbType.DateTime),
					new MySqlParameter("@newwork", MySqlDbType.VarChar,20),
					new MySqlParameter("@newworktime", MySqlDbType.DateTime),
					new MySqlParameter("@newworkuserid", MySqlDbType.Int64,20),
					new MySqlParameter("@taskstatus", MySqlDbType.VarChar,255),
					new MySqlParameter("@newworkusername", MySqlDbType.VarChar,20),
					new MySqlParameter("@dataadduserid", MySqlDbType.Int32,11),
					new MySqlParameter("@datadepid", MySqlDbType.Int32,11),
					new MySqlParameter("@databranchid", MySqlDbType.Int32,11),
					new MySqlParameter("@datadepname", MySqlDbType.VarChar,100),
					new MySqlParameter("@dataaddusername", MySqlDbType.VarChar,100),
					new MySqlParameter("@databranchname", MySqlDbType.VarChar,100),
					new MySqlParameter("@dataaddtime", MySqlDbType.DateTime)};
			parameters[0].Value = model.gid;
			parameters[1].Value = model.type;
			parameters[2].Value = model.taskno;
			parameters[3].Value = model.taskinfo;
			parameters[4].Value = model.tasksctype;
			parameters[5].Value = model.tasksctypename;
			parameters[6].Value = model.taskuserid;
			parameters[7].Value = model.taskmj;
			parameters[8].Value = model.taskcj;
			parameters[9].Value = model.taskendtime;
			parameters[10].Value = model.newwork;
			parameters[11].Value = model.newworktime;
			parameters[12].Value = model.newworkuserid;
			parameters[13].Value = model.taskstatus;
			parameters[14].Value = model.newworkusername;
			parameters[15].Value = model.dataadduserid;
			parameters[16].Value = model.datadepid;
			parameters[17].Value = model.databranchid;
			parameters[18].Value = model.datadepname;
			parameters[19].Value = model.dataaddusername;
			parameters[20].Value = model.databranchname;
			parameters[21].Value = model.dataaddtime;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(hcrainMvc.Model.hsc_task model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update hsc_task set ");
			strSql.Append("gid=@gid,");
			strSql.Append("type=@type,");
			strSql.Append("taskno=@taskno,");
			strSql.Append("taskinfo=@taskinfo,");
			strSql.Append("tasksctype=@tasksctype,");
			strSql.Append("tasksctypename=@tasksctypename,");
			strSql.Append("taskuserid=@taskuserid,");
			strSql.Append("taskmj=@taskmj,");
			strSql.Append("taskcj=@taskcj,");
			strSql.Append("taskendtime=@taskendtime,");
			strSql.Append("newwork=@newwork,");
			strSql.Append("newworktime=@newworktime,");
			strSql.Append("newworkuserid=@newworkuserid,");
			strSql.Append("taskstatus=@taskstatus,");
			strSql.Append("newworkusername=@newworkusername,");
			strSql.Append("dataadduserid=@dataadduserid,");
			strSql.Append("datadepid=@datadepid,");
			strSql.Append("databranchid=@databranchid,");
			strSql.Append("datadepname=@datadepname,");
			strSql.Append("dataaddusername=@dataaddusername,");
			strSql.Append("databranchname=@databranchname,");
			strSql.Append("dataaddtime=@dataaddtime");
			strSql.Append(" where id=@id");
			MySqlParameter[] parameters = {
					new MySqlParameter("@gid", MySqlDbType.VarChar,36),
					new MySqlParameter("@type", MySqlDbType.VarChar,10),
					new MySqlParameter("@taskno", MySqlDbType.VarChar,50),
					new MySqlParameter("@taskinfo", MySqlDbType.VarChar,800),
					new MySqlParameter("@tasksctype", MySqlDbType.Int64,20),
					new MySqlParameter("@tasksctypename", MySqlDbType.VarChar,50),
					new MySqlParameter("@taskuserid", MySqlDbType.Int64,20),
					new MySqlParameter("@taskmj", MySqlDbType.Double),
					new MySqlParameter("@taskcj", MySqlDbType.Double),
					new MySqlParameter("@taskendtime", MySqlDbType.DateTime),
					new MySqlParameter("@newwork", MySqlDbType.VarChar,20),
					new MySqlParameter("@newworktime", MySqlDbType.DateTime),
					new MySqlParameter("@newworkuserid", MySqlDbType.Int64,20),
					new MySqlParameter("@taskstatus", MySqlDbType.VarChar,255),
					new MySqlParameter("@newworkusername", MySqlDbType.VarChar,20),
					new MySqlParameter("@dataadduserid", MySqlDbType.Int32,11),
					new MySqlParameter("@datadepid", MySqlDbType.Int32,11),
					new MySqlParameter("@databranchid", MySqlDbType.Int32,11),
					new MySqlParameter("@datadepname", MySqlDbType.VarChar,100),
					new MySqlParameter("@dataaddusername", MySqlDbType.VarChar,100),
					new MySqlParameter("@databranchname", MySqlDbType.VarChar,100),
					new MySqlParameter("@dataaddtime", MySqlDbType.DateTime),
					new MySqlParameter("@id", MySqlDbType.Int64,20)};
			parameters[0].Value = model.gid;
			parameters[1].Value = model.type;
			parameters[2].Value = model.taskno;
			parameters[3].Value = model.taskinfo;
			parameters[4].Value = model.tasksctype;
			parameters[5].Value = model.tasksctypename;
			parameters[6].Value = model.taskuserid;
			parameters[7].Value = model.taskmj;
			parameters[8].Value = model.taskcj;
			parameters[9].Value = model.taskendtime;
			parameters[10].Value = model.newwork;
			parameters[11].Value = model.newworktime;
			parameters[12].Value = model.newworkuserid;
			parameters[13].Value = model.taskstatus;
			parameters[14].Value = model.newworkusername;
			parameters[15].Value = model.dataadduserid;
			parameters[16].Value = model.datadepid;
			parameters[17].Value = model.databranchid;
			parameters[18].Value = model.datadepname;
			parameters[19].Value = model.dataaddusername;
			parameters[20].Value = model.databranchname;
			parameters[21].Value = model.dataaddtime;
			parameters[22].Value = model.id;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(long id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from hsc_task ");
			strSql.Append(" where id=@id");
			MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int64)
			};
			parameters[0].Value = id;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from hsc_task ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public hcrainMvc.Model.hsc_task GetModel(long id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,gid,type,taskno,taskinfo,tasksctype,tasksctypename,taskuserid,taskmj,taskcj,taskendtime,newwork,newworktime,newworkuserid,taskstatus,newworkusername,dataadduserid,datadepid,databranchid,datadepname,dataaddusername,databranchname,dataaddtime from hsc_task ");
			strSql.Append(" where id=@id");
			MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int64)
			};
			parameters[0].Value = id;

			hcrainMvc.Model.hsc_task model=new hcrainMvc.Model.hsc_task();
			DataSet ds=DbHelperMySQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public hcrainMvc.Model.hsc_task DataRowToModel(DataRow row)
		{
			hcrainMvc.Model.hsc_task model=new hcrainMvc.Model.hsc_task();
			if (row != null)
			{
				if(row["id"]!=null && row["id"].ToString()!="")
				{
					model.id=long.Parse(row["id"].ToString());
				}
				if(row["gid"]!=null)
				{
					model.gid=row["gid"].ToString();
				}
				if(row["type"]!=null)
				{
					model.type=row["type"].ToString();
				}
				if(row["taskno"]!=null)
				{
					model.taskno=row["taskno"].ToString();
				}
				if(row["taskinfo"]!=null)
				{
					model.taskinfo=row["taskinfo"].ToString();
				}
				if(row["tasksctype"]!=null && row["tasksctype"].ToString()!="")
				{
					model.tasksctype=long.Parse(row["tasksctype"].ToString());
				}
				if(row["tasksctypename"]!=null)
				{
					model.tasksctypename=row["tasksctypename"].ToString();
				}
				if(row["taskuserid"]!=null && row["taskuserid"].ToString()!="")
				{
					model.taskuserid=long.Parse(row["taskuserid"].ToString());
				}
					//model.taskmj=row["taskmj"].ToString();
					//model.taskcj=row["taskcj"].ToString();
				if(row["taskendtime"]!=null && row["taskendtime"].ToString()!="")
				{
					model.taskendtime=DateTime.Parse(row["taskendtime"].ToString());
				}
				if(row["newwork"]!=null)
				{
					model.newwork=row["newwork"].ToString();
				}
				if(row["newworktime"]!=null && row["newworktime"].ToString()!="")
				{
					model.newworktime=DateTime.Parse(row["newworktime"].ToString());
				}
				if(row["newworkuserid"]!=null && row["newworkuserid"].ToString()!="")
				{
					model.newworkuserid=long.Parse(row["newworkuserid"].ToString());
				}
				if(row["taskstatus"]!=null)
				{
					model.taskstatus=row["taskstatus"].ToString();
				}
				if(row["newworkusername"]!=null)
				{
					model.newworkusername=row["newworkusername"].ToString();
				}
				if(row["dataadduserid"]!=null && row["dataadduserid"].ToString()!="")
				{
					model.dataadduserid=int.Parse(row["dataadduserid"].ToString());
				}
				if(row["datadepid"]!=null && row["datadepid"].ToString()!="")
				{
					model.datadepid=int.Parse(row["datadepid"].ToString());
				}
				if(row["databranchid"]!=null && row["databranchid"].ToString()!="")
				{
					model.databranchid=int.Parse(row["databranchid"].ToString());
				}
				if(row["datadepname"]!=null)
				{
					model.datadepname=row["datadepname"].ToString();
				}
				if(row["dataaddusername"]!=null)
				{
					model.dataaddusername=row["dataaddusername"].ToString();
				}
				if(row["databranchname"]!=null)
				{
					model.databranchname=row["databranchname"].ToString();
				}
				if(row["dataaddtime"]!=null && row["dataaddtime"].ToString()!="")
				{
					model.dataaddtime=DateTime.Parse(row["dataaddtime"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,gid,type,taskno,taskinfo,tasksctype,tasksctypename,taskuserid,taskmj,taskcj,taskendtime,newwork,newworktime,newworkuserid,taskstatus,newworkusername,dataadduserid,datadepid,databranchid,datadepname,dataaddusername,databranchname,dataaddtime ");
			strSql.Append(" FROM hsc_task ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM hsc_task ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from hsc_task T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			MySqlParameter[] parameters = {
					new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@PageSize", MySqlDbType.Int32),
					new MySqlParameter("@PageIndex", MySqlDbType.Int32),
					new MySqlParameter("@IsReCount", MySqlDbType.Bit),
					new MySqlParameter("@OrderType", MySqlDbType.Bit),
					new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
					};
			parameters[0].Value = "hsc_task";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

